package io.wins.vertx.service.impl;

import io.vertx.core.Vertx;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.wins.vertx.entity.Seat;
import io.wins.vertx.service.BaseService;
import io.wins.vertx.service.SeatService;

public class SeatServiceImpl extends BaseService implements SeatService {
    public SeatServiceImpl(final Vertx vertx) {
        this.vertx = vertx;
    }

    @Override
    public SeatService getSeatById(final String id, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_seat where id = ?")
            .put(PARAMS, new JsonArray().add(id));
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public SeatService listSeat(final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_seat where is_delete = '0' order by updated_on desc");
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public SeatService createSeat(final Seat seat, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "insert into ds_seat (id, name, `rows`, `columns`, maps, legend, seats, unavailables, created_on, updated_on) VALUES (?,?,?,?,?,?,?,?, UNIX_TIMESTAMP(), UNIX_TIMESTAMP())")
            .put(PARAMS, new JsonArray()
                .add(seat.getId())
                .add(seat.getName())
                .add(seat.getRows())
                .add(seat.getColumns())
                .add(seat.getMaps())
                .add(seat.getLegend())
                .add(seat.getSeats())
                .add(seat.getUnavailables()));
        this.insertSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }

    @Override
    public SeatService updateSeat(final Seat seat, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_seat set name=?, `rows`=?, `columns`=?, maps=?, legend=?, seats=?, unavailables=?, updated_on = UNIX_TIMESTAMP() where id = ?")
            .put(PARAMS, new JsonArray()
                .add(seat.getName())
                .add(seat.getRows())
                .add(seat.getColumns())
                .add(seat.getMaps())
                .add(seat.getLegend())
                .add(seat.getSeats())
                .add(seat.getUnavailables())
                .add(seat.getId()));
        this.insertSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }

    @Override
    public SeatService deleteSeat(final String id, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_seat set is_delete = '1' where id = ?")
            .put(PARAMS, new JsonArray().add(id));
        this.deleteSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }
}
